<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>上传文件 </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" >
    <link rel="stylesheet" href="bootstrap/css/bootstrap.css">
    <!--[if lte IE 6]>
       <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-ie6.css">
    <![endif]-->
    <!--[if lte IE 7]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/ie.css">
    <![endif]-->
    <link rel="stylesheet" href="leipi.style.css">
    <link rel="stylesheet" href="css/form.design.css">
    <script type="text/javascript" src="../dialogs/internal.js"></script>
    <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="../../../js/utils.js"></script>
    <script type="text/javascript">
/* Thank you by  
http://www.alt-tag.com/blog/2006/02/ie-dom-bugs/ */

var rootPath = $("#root-path",parent.document).val();

function createElement(type, name) {     
    var element = null;     
    try {        
        element = document.createElement('<'+type+' name="'+name+'">');     
    } catch (e) {}   
    if(element==null) {     
        element = document.createElement(type);     
        element.name = name;     
    } 
    return element;     
}
    </script>
</head>
<body>
<div class="content">
    <table class="table table-bordered">
     <tr>
        <th><span>标题</span><span class="label label-important">*</span></th>
        <td><input type="text" class="require" id="orgtitle" /></td>
        <th><span>必填字段</span></th>
        <td><label class="checkbox inline"><input id="fieldrequire" type="checkbox"/>是</label></td>
    </tr>
    <tr>
        <th><span>绑定表</span><span class="label label-important">*</span></th>
        <td>
           <select id="bind_table" class="require">
              <option class="" value="">无</option>
              <option class="cnoj-dyn-opt" value="">正在加载数据</option>
           </select>
        </td>
        <th><span>绑定字段</span><span class="label label-important">*</span></th>
        <td>
           <select id="bind_table_field" class="require">
           	   <option class="" value="">无</option>
               <option class="cnoj-dyn-opt" value="">正在加载数据</option>
           </select>
        </td>
    </tr>
    
    <tr>
       <th><span>宽度(px)</span></th>
       <td><input id="orgwidth" type="text" value="200" placeholder="auto"/></td>
       <th><span>可见性</span></th>
       <td>
         <label class="checkbox inline"><input id="orghide" type="checkbox"/> 隐藏 </label>
       </td>
    </tr>
    <tr>
      <th>关联字段</th>
      <td>
      	<select id="relate_field">
      		<option value="">请选择</option>
      	</select>
      </td>
      <th>关联值</th>
      <td><input id="relate_field_value" type="text"/></td>
    </tr>
     <tr>
         <th><span>记录日志</span></th>
         <td colspan="3">
             <label class="checkbox inline"><input id="is-log" type="checkbox"/> 是 </label>
         </td>
     </tr>
    <tr>
        <th>上传文件类型</th>
        <td colspan="3">
        <textarea rows="3" style="width: 95%" class="form-control" id="filetype">jpg,png,gif,doc,docx,xls,xlsx,ppt,pptx,pdf,zip,rar,txt</textarea>
        <p class="help-block" style="color: #737373;">填写文件后缀，为空表示支持所有文件格式，多个文件类型之间用英文逗号分隔；如: jpg,docx,pdf.</p>
        </td>
    </tr>
   </table>
</div>
<script type="text/javascript">
var oNode = null,thePlugins = 'file';
var tableDefValue = null,fieldDefValue = null;
window.onload = function() {
	var tableFieldId = null;
    if( UE.plugins[thePlugins].editdom ){
        oNode = UE.plugins[thePlugins].editdom;
        oNode.setAttribute('name','leipiNewField');
		var gRequrie = oNode.getAttribute('fieldrequire'),
		gTitle=oNode.getAttribute('title').replace(/&quot;/g,"\""),
		gHidden=oNode.getAttribute('orghide'),
		gRelateField = oNode.getAttribute('relate_field'),
		gRelateFieldValue = oNode.getAttribute('relate_field_value'),
		gBindTable = oNode.getAttribute('bind_table'),gBindField=oNode.getAttribute('bind_table_field'),
		gWidth=oNode.getAttribute('orgwidth');
        var gFileType = oNode.getAttribute('filetype');
        var gIsLog = oNode.getAttribute('is_log');

        gTitle = gTitle==null ? '' : gTitle;
        tableDefValue = gBindTable==null?'':gBindTable;
        fieldDefValue = gBindField==null?'':gBindField;
        tableFieldId = fieldDefValue;
        $G('orgtitle').value = gTitle;
        $G('filetype').value = gFileType;
        $G('relate_field_value').value = utils.handleNull(gRelateFieldValue);
        if (gRequrie == '1') {
            $G('fieldrequire').checked = true;
        }
        if (gHidden == '1') {
            $G('orghide').checked = true;
        }
        if(gIsLog == '1') {
            $G('is-log').checked = true;
        }
        $G('orgwidth').value = gWidth;
    } 
    utils.selectItem("#bind_table",rootPath+'/form/table/item.json',tableDefValue,function(val){
    	utils.selectCascadeItem("#bind_table_field", "#bind_table", rootPath+'/form/table/fields.json', "id", fieldDefValue, null);
	});
	//获取编辑器中的内容
	var ueditorContent = parent.getUeditorContents();
	if(utils.isNotEmpty(ueditorContent)) {
		var $uc = $(ueditorContent);
		$("#relate_field .relate_field_option").remove();
		$uc.find("select[leipiplugins=select]").each(function(){
			var $this = $(this);
			var pTableFieldId = $this.attr("bind_table_field");
			var title = $this.attr("orgtitle");
			if(utils.isEmpty(tableFieldId) || tableFieldId != pTableFieldId) {
				$("#relate_field").append("<option class='relate_field_option' value='"+pTableFieldId+"'>"+title+"</option>");
			}
		});
	}
	$G('relate_field').value = utils.handleNull(gRelateField);
}
dialog.oncancel = function () {
    if( UE.plugins[thePlugins].editdom ) {
        delete UE.plugins[thePlugins].editdom;
    }
};
dialog.onok = function (){
	var isCheck = true;
	var msg = null;
	$(".require").each(function(){
		if(utils.isEmpty($(this).val())) {
			var title = $(this).parent().prev().find("span:first").text();
			msg = title+"不能为空！";
			isCheck = false;
			$(this).focus();
			return false;
		}
	});
	if(!isCheck) {
		alert(msg);
		return false;
	}
    if( !oNode ) {
        try {
            oNode = createElement('input','leipiNewField');
            oNode.setAttribute('type','file');
            oNode.setAttribute('name','leipiNewField');
            oNode.setAttribute('leipiPlugins',thePlugins);
            setAttr(oNode);
            editor.execCommand('insertHtml',oNode.outerHTML);
        } catch (e) {
            try {
                editor.execCommand('error');
            } catch ( e ) {
                alert('控件异常，请联系技术支持');
            }
            return false;
        }
    } else {
    	oNode.setAttribute('name','leipiNewField');
    	setAttr(oNode);
        delete UE.plugins[thePlugins].editdom;
    }
};

/**
 * 
 */
function setAttr(oNode) {
	var gTitle=$G('orgtitle').value.replace(/\"/g,"&quot;");
    var gWidth=$G('orgwidth').value;
    var bindTable = $G('bind_table').value;
    var bindTableField = $G('bind_table_field').value;
    var fileType = $G('filetype').value;
    var relateField = $G('relate_field').value;
    var gRelateFieldValue = $G('relate_field_value').value;
	oNode.setAttribute('title',gTitle);
    oNode.setAttribute('bind_table',bindTable);
    oNode.setAttribute('bind_table_field',bindTableField);
    oNode.setAttribute('filetype',fileType);
    
    oNode.setAttribute('id',bindTable+"-"+bindTableField);
    oNode.setAttribute('relate_field',utils.handleNull(relateField));
    oNode.setAttribute('relate_field_value',utils.handleNull(gRelateFieldValue));
    
    if ($G('orghide').checked ) {
        oNode.setAttribute('orghide',1);
    } else {
        oNode.setAttribute('orghide',0);
    }
    if ($G('fieldrequire').checked ) {
        oNode.setAttribute('fieldrequire',1);
        var className = oNode.className;
        if(typeof(className) === 'undefined' || null == className) {
        	className = '';
        }
        oNode.className=className+" require";
    } else {
        oNode.setAttribute('fieldrequire',0);
        var className = oNode.className;
        if(typeof(className) === 'undefined' || null == className) {
        	className = '';
        } else {
        	className = className.replace(" require", "");
        }
        oNode.className=className;
    }
    if( gWidth != '' ) {
        oNode.style.width = gWidth+ 'px';
        oNode.setAttribute('orgwidth',gWidth );
    } else {
    	oNode.style.width = null;
    	oNode.setAttribute('orgwidth', '');
    }
    if($G('is-log').checked) {
        oNode.setAttribute('is_log',1);
    } else {
        oNode.setAttribute('is_log',0);
    }
}
</script>
</body>
</html>